<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('板块统计')" />
<!--	<th:block th:include="include :: select2-css" />-->
<!--	<th:block th:include="include :: bootstrap-select-css" />-->
</head>
<body class="gray-bg">
<div class="container-div">
	<div class="row">
		<div class="col-sm-12 select-table table-striped">
			<div class="select-list">
				<ul>
					<li>
						<select class="form-control"   id="bk-picker">
						</select>
					</li>
					<li>
						<a class="btn btn-primary btn-rounded btn-sm" onclick="bkConfirm()"><i class="fa fa-search"></i>&nbsp;确定</a>
<!--						<a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>-->
						<a class="btn btn-success btn-rounded btn-sm" onclick="add()"><i class="fa fa-plus"></i>&nbsp;新增</a>
						<a class="btn btn-warning btn-rounded btn-sm" onclick="saveBk()"><i class="fa fa-circle-o"></i> 保存</a>
					</li>
				</ul>
			</div>
			<table class="table table-hover no-margins" >
				<thead>
				<tr>
					<th style="width:70px">板块</th>
					<th style="width:50px">类型</th>
					<th>股票</th>
					<th>阶段</th>
					<th>备注</th>
					<th style="width:500px">修改</th>
					<th style="width:80px">操作</th>
				</tr>
				</thead>
				<tbody id="hot-concept-body">

				</tbody>
			</table>
			<div class="select-list">
				<ul>
					<li>
						<input type="text" placeholder="请输入概念" class="form-control" id = "hotConceptSearch">
					</li>
					<li>
						<a class="btn btn-success btn-rounded btn-sm" onclick="searchHotStocks()"><i class="fa fa-search"></i>&nbsp;查询</a>
					</li>
					<li>
						<textarea  id="chooseBoard" style="width: 300px" rows='10'></textarea>
					</li>
				</ul>

			</div>
		</div>
	</div>
	<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
					<h4 class="modal-title" id="addModalLabel">新增板块</h4>
				</div>
				<div class="modal-body">
					<form>
						<div class="form-group">
							<label for="recipient-name" class="control-label">板块:</label>
							<input type="text" class="form-control" id="recipient-name" >
						</div>
					</form>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
					<button type="button" class="btn btn-primary" id="addConceptBtn">确定</button>
				</div>
			</div>
		</div>
	</div>
</div>
<th:block th:include="include :: footer" />
<!--<th:block th:include="include :: select2-js" />-->
<!--<th:block th:include="include :: bootstrap-select-js" />-->
<script th:inline="javascript">
	var recentConceptNames = [[${recentConceptNames}]];
	var recentConceptStocks = [[${recentConceptStocks}]];
	var dailyHotBkStocksList = [[${dailyHotBkStocksList}]];
	var showRecentConceptStocks=[]

	initSelect(recentConceptNames)

	function initSelect(conceptNames){
		$("#bk-picker").empty();
		for(var i in conceptNames){
			var name =conceptNames[i];
			$("#bk-picker").append('<option value="'+name+'">'+name+'</option>');
		}
	}

	function bkConfirm(){
		var name=$("#bk-picker").val();
		var showRecentConceptStock={"name":name,"data":recentConceptStocks[name]};
		for(var i in showRecentConceptStocks ){
			if(showRecentConceptStocks[i]["name"]==name){
				freshTable()
				return
			}
		}
		showRecentConceptStocks.push(showRecentConceptStock);
		freshTable();
	}

	function freshTable(){
		$("#hot-concept-body").empty();
		for(var i in showRecentConceptStocks ){
			appendOne(showRecentConceptStocks[i])
		}
	}


	function appendOne(showRecentConceptStock){
		var main_stocks="";
		// var trend="";
		var lb="";
		// var taoli="";
		var other="";
		var data = showRecentConceptStock["data"];
		data = JSON.parse(data);
		for(var i in data["main_stock"]){
			main_stocks = main_stocks+'<span class="label label-success">'+data["main_stock"][i]+'</span> '
		}
		// for(var i in data["trend"]){
		// 	trend = trend+'<span class="label label-success">'+data["trend"][i]+'</span> '
		// }
		for(var i in data["lb"]){
			lb = lb+'<span class="label label-success">'+data["lb"][i]+'</span> '
		}
		// for(var i in data["taoli"]){
		// 	taoli = taoli+'<span class="label label-success">'+data["taoli"][i]+'</span> '
		// }
		for(var i in data["other"]){
			other = other+'<span class="label label-success">'+data["other"][i]+'</span> '
		}
		var needAppend = "<tr><td rowspan=\"3\"><span class=\"label label-danger\">"+showRecentConceptStock["name"]+"</span></td>"+
				"<td> <span class=\"label label-warning\">主要</span></td> <td>"+main_stocks+" </td>"+
				"<td rowspan=\"3\"><textarea   rows='10'>"+data["jieduan"]+"</textarea></td>"+
				"<td rowspan=\"3\"><textarea   rows='10'>"+data["comment"]+"</textarea></td>"+
				"<td rowspan=\"3\"><textarea   rows='10' cols='60'>"+JSON.stringify(data)+"</textarea></td>"+
				"<td rowspan=\"3\"><a class=\"btn btn-info btn-xs \" href=\"javascript:void(0)\" onclick=\"edit(this)\">确定</a></td></tr>";
		// needAppend=needAppend+"<tr><td> <span class=\"label label-primary\">趋势</span></td> "+
		// 		"<td>"+trend+" </td></tr>";
		needAppend=needAppend+"<tr><td> <span class=\"label label-primary\">连板</span></td> "+
				"<td>"+lb+" </td></tr>";
		// needAppend=needAppend+"<tr><td> <span class=\"label label-warning\">套利</span></td> "+
		// 		"<td>"+taoli+" </td></tr>";
		needAppend=needAppend+"<tr><td> <span class=\"label label-primary\">其他</span></td> "+
				"<td>"+other+" </td></tr>";
		$("#hot-concept-body").append(needAppend)
	}


	$("#addConceptBtn").click(function () {
		var concept = $("#recipient-name").val();
		$('#addModal').modal('hide');
		var data00="{\n" +
				"  \"main_stock\": [\"\",\"\"],\n" +
				"  \"lb\": [\"\",\"\"],\n" +
				// "  \"taoli\": [\"\",\"\"],\n" +
				"  \"other\": [\"\",\"\"],\n" +
				"  \"jieduan\": \"\",\n" +
				"  \"comment\": \"\"\n" +
				"}";
		$.operate.post(ctx + "bk/recentConcept/edit2", {"concept":concept,"data":data00},function(data){
			recentConceptNames.push(concept)
			recentConceptStocks[concept]=data00
			// $("#bk-picker").append('<option value="'+concept+'">'+concept+'</option>');
			// $('.bk-picker').val(null).trigger('change');
			initSelect(recentConceptNames);
		});

	})

	function edit(ele) {
		var prevSiblingContent = $(ele).parent().prev().find('textarea:first').val();
		var comment = $(ele).parent().prev().prev().find('textarea:first').val();
		var jieduan = $(ele).parent().prev().prev().prev().find('textarea:first').val();
		var name = $(ele).parent().prev().prev().prev().prev().prev().prev().text();
		var needChange = JSON.parse(prevSiblingContent);
		needChange["comment"]=comment;
		needChange["jieduan"]=jieduan;
		needChange =JSON.stringify(needChange);
		$.operate.post(ctx + "bk/recentConcept/edit2", {"concept":name,"data":needChange},function(data){
			recentConceptStocks[name]=needChange;
			var showRecentConceptStock={"name":name,"data":recentConceptStocks[name]};
			for(var i in showRecentConceptStocks ){
				if(showRecentConceptStocks[i]["name"]==name){
					showRecentConceptStocks[i]=showRecentConceptStock
					freshTable()
					return
				}
			}
		});
	}

	function saveBk() {
		console.log(1)
		$.operate.post(ctx + "bk/recentConcept/saveDaily", {"data":JSON.stringify(showRecentConceptStocks)},function(data){
			$.modal.msgSuccess('保存成功！');
		});
	}

	function add() {
		$("#stock-name-label").text("")
		$("#add-concepts").val("")
		$("#recipient-name").val("")
		// $("#recipient-index").val('-1')
		$('#addModal').modal('show');
	}


	function searchHotStocks(){
		var lastTextarea = $('#hot-concept-body').find('textarea').last();
		console.log(lastTextarea)
		var con_stocks = JSON.parse(lastTextarea.val())
		console.log(con_stocks)

		var concept=$("#hotConceptSearch").val()
		var result = ""
		for (const index in dailyHotBkStocksList) {
			var hotBkStock = dailyHotBkStocksList[index];
			if(hotBkStock['plates'].indexOf(concept)>-1){
				if(hotBkStock['upLimit']==1){
					con_stocks['lb'].push(hotBkStock["prodName"])
				}
				if(hotBkStock['upLimit']==0){
					con_stocks['other'].push(hotBkStock["prodName"])
				}
				result=result+'"'+(hotBkStock["prodName"])+'",'
			}
		}
		con_stocks['lb'] = con_stocks['lb'].filter((item, index) => con_stocks['lb'].indexOf(item) === index);
		con_stocks['other'] = con_stocks['other'].filter((item, index) => con_stocks['other'].indexOf(item) === index);
		lastTextarea.val(JSON.stringify(con_stocks))
		$("#chooseBoard").val(result)

	}

</script>
</body>
</html>